package com.redmoon.oa.flow;

import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import com.cloudwebsoft.framework.base.QObjectDb;
import com.cloudwebsoft.framework.db.JdbcTemplate;

import cn.js.fan.db.ListResult;
import cn.js.fan.db.ResultIterator;
import cn.js.fan.db.ResultRecord;
import cn.js.fan.db.SQLFilter;
import cn.js.fan.util.DateUtil;
import cn.js.fan.util.ParamUtil;
import cn.js.fan.util.ResKeyException;
import cn.js.fan.util.StrUtil;
import com.cloudwebsoft.framework.util.LogUtil;

public class AttachmentLogDb extends QObjectDb {
	public static final int TYPE_DOWNLOAD = 0;
	public static final int TYPE_EDIT = 1;
	
	public static String getTypeDesc(int type) {
		if (type==TYPE_DOWNLOAD) {
			return "下载";
		}
		else {
			return "修改";
		}
	}
	
	public static int getDownloadCount(String userName, long attId) {
		String sql = "select count(*) from flow_document_attach_log where log_type=" + TYPE_DOWNLOAD + " and att_id=? and user_name=?";
		JdbcTemplate jt = new JdbcTemplate();
		ResultIterator ri;
		try {
			ri = jt.executeQuery(sql, new Object[]{attId, userName});
			if (ri.hasNext()) {
				ResultRecord rr = (ResultRecord)ri.next();
				return rr.getInt(1);
			}
		} catch (SQLException e) {
			LogUtil.getLog(AttachmentLogDb.class).error(e);
		}

		return 0;
	}
	
	public boolean log(String userName, long flowId, long attId, int logType) {
		boolean re = false;
		try {
			re = create(new JdbcTemplate(), new Object[]{userName,flowId,attId,logType,new Date()});
		} catch (ResKeyException e) {
			LogUtil.getLog(getClass()).error(e);
		} 
		return re;
	}
	
	/**
	 * 查询
	 * @param request
	 * @param flowId
	 * @param attId
	 * @return
	 */
	public String getQuery(HttpServletRequest request, long flowId, long attId) {
		String orderBy = ParamUtil.get(request, "field");
		String sort = ParamUtil.get(request, "order");
		if ("ascend".equals(sort)) {
			sort = "asc";
		}
		else {
			sort = "desc";
		}

		if ("".equals(orderBy)) {
			orderBy = "id";
		}
		
		if ("logTime".equals(orderBy)) {
			orderBy = "log_time";
		}
		else if ("flowId".equals(orderBy)) {
			orderBy = "flow_id";
		}
		else if ("logType".equals(orderBy)) {
			orderBy = "log_type";
		}
		else if ("attName".equals(orderBy)) {
			orderBy = "att_id";
		}

		StringBuffer sb = new StringBuffer();
		String realName = ParamUtil.get(request, "realName");
		String attName = ParamUtil.get(request, "attName");
		String logTimeBegin = ParamUtil.get(request, "beginDate");
		String logTimeEnd = ParamUtil.get(request, "endDate");
		String flowTitle = ParamUtil.get(request, "flowTitle");
		int logType = ParamUtil.getInt(request, "logType", -1);
		sb = new StringBuffer();
		sb.append("select l.id from flow_document_attach_log l");
		if (!"".equals(realName)) {
			sb.append(", users u");
		}
		if (!"".equals(flowTitle)) {
			sb.append(", flow f");
		}
		if (!"".equals(attName)) {
			sb.append(", flow_document_attach a");
		}
		sb.append(" where 1=1");
		if (!"".equals(realName)) {
			sb.append(" and u.name=l.user_name");
		}
		if (!"".equals(flowTitle)) {
			sb.append(" and f.id = l.flow_id");
		}
		if (!"".equals(attName)) {
			sb.append(" and a.id=l.att_id");
		}

		if (logType != -1) {
			sb.append(" and log_type=" + logType);
		}
		if (attId != -1) {
			sb.append(" and att_id=" + attId );
		}
		if (flowId != -1) {
			sb.append(" and l.flow_id=" + flowId);
		}
		if (!"".equals(realName)) {
			sb.append(" and u.realname like " + StrUtil.sqlstr("%" + realName + "%"));
		}
		if (!"".equals(attName)) {
			sb.append(" and a.name like " + StrUtil.sqlstr("%" + attName + "%"));
		}
		if (!"".equals(flowTitle)) {
			sb.append(" and f.title like " + StrUtil.sqlstr("%" + flowTitle + "%"));
		}
		if (!"".equals(logTimeBegin)) {
			sb.append(" and log_time>=" + SQLFilter.getDateStr(logTimeBegin, "yyyy-MM-dd"));
		}
		if (!"".equals(logTimeEnd)) {
			Date d = DateUtil.parse(logTimeEnd, "yyyy-MM-dd");
			if (d!=null) {
				// 结束日期需加1，以符合日常习惯
				d = DateUtil.addDate(d, 1);
				String strDate = DateUtil.format(d, "yyyy-MM-dd");
				sb.append(" and log_time<" + SQLFilter.getDateStr(strDate, "yyyy-MM-dd"));
			}
		}
		sb.append(" order by " + orderBy + " " + sort);
		return sb.toString();
	}	
}
